<template>
  <div class="wrapper">
    <!-- header部分 -->
    <header>
      <p>地址管理</p>
    </header>
    <!-- 地址列表部分 -->
    <ul class="addresslist">
      <li v-for="item in deliveryAddressArr">
        <div class="addresslist-left" @click="setDeliveryAddress(item)">
          <h3>{{ item.contactName }}{{ item.contactSex == 1 ? '先生' : '女士' }} {{ item.contactTel }}
          </h3>
          <p>{{ item.address }}</p>
        </div>
        <div class="addresslist-right">
          <i class="fa fa-edit" @click="editUserAddress(item.id)"></i>
          <i class="fa fa-remove" @click="removeUserAddress(item.id)"></i>
        </div>
      </li>
    </ul>
    <!-- 新增地址部分 -->
    <div class="addbtn" @click="toAddUserAddress">
      <i class="fa fa-plus-circle"></i>
      <p>新增收货地址</p>
    </div>
    <!-- 底部导航 -->
    <footer class="bottom-nav">
      <div class="nav-item" :class="{ active: $route.path === '/index' }" @click="$router.push('/index')">
        <i class="fa fa-home"></i>
        <span>首页</span>
      </div>
      <div class="nav-item">
        <i class="fa fa-compass"></i>
        <span>商家</span>
      </div>
      <div class="nav-item" :class="{ active: $route.path === '/orderList' }" @click="$router.push('/orderList')">
        <i class="far fa-file-alt"></i>
        <span>订单</span>
      </div>
      <div class="nav-item" :class="{ active: $route.path === '/userProfile' }" @click="$router.push('/userProfile')">
        <i class="fa fa-user"></i>
        <span>我的</span>
      </div>
    </footer>
  </div>
</template>
<script>
import Footer from '../components/Footer.vue';

export default {
  name: 'UserAddress',
  data() {
    return {
      id: this.$route.query.businessId,
      user: {},
      deliveryAddressArr: []
    }
  },
  created() {
    //获取token
    this.token = localStorage.getItem('token');

    //获取user
    this.$axios.get(`/user`, {
      headers: {
        'Authorization': `Bearer ${this.token}`
      }
    }).then(response => {
      this.user = response.data.data;
      this.listDeliveryAddressByUserId();
    }).catch(error => {
      console.error(error);
    });
  },
  components: {
    Footer
  },
  methods: {
    listDeliveryAddressByUserId() {
      //查询送货地址
      this.$axios.get(`/addresses/${this.user.id}/user`, {
        headers: {
          'Authorization': `Bearer ${this.token}`
        }
      }).then(response => {
        this.deliveryAddressArr = response.data.data;
      }).catch(error => {
        console.error(error);
      });
    },

    setDeliveryAddress(deliveryAddress) {
      //把用户选择的默认送货地址存储到localStorage中
      this.$setLocalStorage(this.user.id, deliveryAddress);
      this.$router.push({ path: '/orders', query: { businessId: this.id } });
    },
    toAddUserAddress() {
      this.$router.push({ path: '/addUserAddress', query: { businessId: this.id } });
    },

    editUserAddress(daId) {
      this.$router.push({
        path: '/editUserAddress', query:
          { businessId: this.id, daId: daId }
      });
    },
    removeUserAddress(daId) {
      if (!confirm('确认要删除此送货地址吗？')) {
        return;
      }


      this.$axios.delete(`/addresses/${daId}`, {
        headers: {
          'Authorization': `Bearer ${this.token}`
        }
      }).then(response => {
        if (response.data.data > 0) {
          let deliveryAddress = this.$getLocalStorage(this.user.id);
          if (deliveryAddress != null && deliveryAddress.id == daId) {
            this.$removeLocalStorage(this.user.id);
          }
          this.listDeliveryAddressByUserId();
        } else {
          alert('删除地址失败！');
        }
      }).catch(error => {
        console.error(error);
      });
    }
  }
}
</script>
<style scoped>
/*************** 总容器 ***************/
.wrapper {
  width: 100%;
  height: 100%;
  background-color: #F5F5F5;
}

/*************** header ***************/
.wrapper header {
  width: 100%;
  height: 12vw;
  background-color: #0097FF;
  display: flex;
  justify-content: space-around;
  align-items: center;
  color: #fff;
  font-size: 4.8vw;
  position: fixed;
  left: 0;
  top: 0;
  /*保证在最上层*/
  z-index: 1000;
}

/*************** addresslist ***************/
.wrapper .addresslist {
  width: 100%;
  margin-top: 12vw;
  background-color: #fff;
}

.wrapper .addresslist li {
  width: 100%;
  box-sizing: border-box;
  border-bottom: solid 1px #DDD;
  padding: 3vw;
  display: flex;
}

.wrapper .addresslist li .addresslist-left {
  flex: 5;
  /*左边这块区域是可以点击的*/
  user-select: none;
  cursor: pointer;
}

.wrapper .addresslist li .addresslist-left h3 {
  font-size: 4.6vw;
  font-weight: 300;
  color: #666;
}

.wrapper .addresslist li .addresslist-left p {
  font-size: 4vw;
  color: #666;
}

.wrapper .addresslist li .addresslist-right {
  flex: 1;
  font-size: 5.6vw;
  color: #999;
  cursor: pointer;
  display: flex;
  justify-content: space-around;
  align-items: center;
}

/*************** 新增地址部分 ***************/
.wrapper .addbtn {
  width: 100%;
  height: 14vw;
  border-top: solid 1px #DDD;
  border-bottom: solid 1px #DDD;
  background-color: #fff;
  margin-top: 4vw;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 4.5vw;
  color: #0097FF;
  user-select: none;
  cursor: pointer;
}

.wrapper .addbtn p {
  margin-left: 2vw;
}

/* 底部导航 */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 16vw;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-top: 1px solid #e0e0e0;
  display: flex;
  justify-content: space-around;
  align-items: center;
  z-index: 100;
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: #999;
  font-size: 3vw;
  cursor: pointer;
  transition: all 0.3s ease;
  padding: 2vw;
  border-radius: 10px;
}

.nav-item.active {
  color: #0097FF;
  transform: translateY(-3px);
}

.nav-item i {
  font-size: 5.5vw;
  margin-bottom: 1vw;
  transition: all 0.3s ease;
}

.nav-item.active i {
  transform: scale(1.2);
}

.nav-item span {
  font-size: 3vw;
  font-weight: 500;
}
</style>